<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股东名称" prop="investorName">
        <el-input
          v-model="queryParams.investorName"
          placeholder="请输入股东名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:investor:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="investorList">
      <el-table-column :formatter="defaultTableColumnDate" label="ID" align="center" prop="id" />
      <el-table-column :formatter="defaultTableColumnDate" label="股东名称" align="center" prop="investorName" />
      <el-table-column :formatter="defaultTableColumnDate" label="社会信用代码" align="center" prop="socialCode" />
      <el-table-column :formatter="defaultTableColumnDate" label="联系人" align="center" prop="contact" />
      <el-table-column :formatter="defaultTableColumnDate" label="手机号" align="center" prop="phoneNumber" />
      <el-table-column :formatter="defaultTableColumnDate" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.investor_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="更新人" align="center" prop="updateBy" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:investor:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:investor:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleForm(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股东/合伙人管理对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="股东类型" prop="investorType">
          <el-radio-group v-model="form.investorType">
            <el-radio
              v-for="dict in dict.type.sc_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="股东名称" prop="investorName">
          <el-input v-model="form.investorName" placeholder="请输入股东名称" />
        </el-form-item>
        <el-form-item label="社会信用代码" prop="socialCode" v-if="form.investorType == '企业'">
          <el-input v-model="form.socialCode" placeholder="请输入社会信用代码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="股东简介" prop="investorDesc">
          <el-input v-model="form.investorDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.investor_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看股东/合伙人管理对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="formopen" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :disabled="true" label-width="180px">
        <el-form-item label="股东名称" prop="investorName">
          <el-input v-model="form.investorName" placeholder="请输入股东名称" />
        </el-form-item>
        <el-form-item label="社会信用代码" prop="socialCode" v-if="form.investorType == '企业'">
          <el-input v-model="form.socialCode" placeholder="请输入社会信用代码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="股东简介" prop="investorDesc">
          <el-input v-model="form.investorDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div style="padding: 50px;">
        <p class="form-span-title">签署的合同</p>
        <div class="gwContracts-item" v-for="item in form.gwContracts">
          <div>
            <div class="title">{{ item.contractName }}</div>
            <div class="time">{{ item.createTime }}</div>
          </div>
          <div class="status">{{ item.statusName }}</div>
        </div>
        <el-empty v-if="form.gwContracts && form.gwContracts.length == 0" description="暂无数据"></el-empty>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInvestor, getInvestor, delInvestor, addInvestor, updateInvestor } from "@/api/system/investor";

export default {
  name: "Investor",
  dicts: ['sc_type', 'investor_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股东/合伙人管理表格数据
      investorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      formopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        investorType: null,
        investorName: null,
        socialCode: null,
        contact: null,
        status: null,
        phoneNumber: null,
        investorDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        investorType: [
          { required: true, trigger: "blur", message: "请选择股东类型" }
        ],
        investorName: [
          { required: true, trigger: "blur", message: "请输入股东名称" }
        ],
        socialCode: [
          { required: true, trigger: "blur", message: "请输入社会信用代码" }
        ],
        contact: [
          { required: true, trigger: "blur", message: "请输入联系人" }
        ],
        phoneNumber: [
          { required: true, trigger: "blur", message: "请输入手机号" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询股东/合伙人管理列表 */
    getList() {
      this.loading = true;
      listInvestor(this.queryParams).then(response => {
        this.investorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.formopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        investorType: null,
        investorName: null,
        socialCode: null,
        contact: null,
        status: '在册',
        phoneNumber: null,
        delFlag: null,
        investorDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加股东/合伙人管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInvestor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改股东/合伙人管理";
      });
    },
    /** 修改按钮操作 */
    handleForm(row) {
      this.reset();
      const id = row.id || this.ids
      getInvestor(id).then(response => {
        this.form = response.data;
        this.formopen = true;
        this.title = "查看股东/合伙人管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInvestor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvestor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('删除后不可恢复，是否确认删除？').then(function() {
        return delInvestor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/investor/export', {
        ...this.queryParams
      }, `investor_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
